<?php
include_once getenv('DOCUMENT_ROOT').'/evento/DAO/DAOFactory.class.php';
include_once getenv('DOCUMENT_ROOT').'/evento/VOB/VOBFactory.class.php';

class ItemAction {


    public function obterPorId(Item $objItem) {

        $DAOf = new DAOFactory();
        $objDAOItem = $DAOf->getItemDAO();

        $rs = $objDAOItem->listar("ide_item = '{$objItem->getId()}'");

        $i = null;

        if ($rs != null) {
            foreach ($rs as $array){
                $VOBf = new VOBFactory();
                $i = $VOBf->getItem();
                $i = $this->copiarPropriedades($array,$i);
            }
        }
//            echo "<pre>";
//            var_dump($i);
//            echo "</pre>";
            return $i;
    }

    public function listar($where = "",$order = "",$inPage = "", $porPagina= ""){

        $lista = null;
        $pRegistro = null;

        if ($inPage != null || $porPagina != null)
            $pRegistro = ($inPage*$porPagina) - $porPagina;

		$DAOf = new DAOFactory();
		$objDAOItem = $DAOf->getItemDAO();

		$rs = $objDAOItem->listar($where,$order,$porPagina,$pRegistro);

		if($rs != null){
                    foreach ($rs as $array){
                        $VOBf = new VOBFactory();
                        $e = $VOBf->getItem();
                        $e = $this->copiarPropriedades($array, $e);
                        $lista[] = $e;
                    }
		}
		return $lista;
	}

       
    public function inserir(Item $objItem){

        
    
        $flag = true;

        $DAOf = new DAOFactory();
        $objDAOItem = $DAOf->getItemDAO();

        //$flag = $this->validarInserir($objItem);

        if($flag){
            $flag = $objDAOItem->inserir($objItem);
        }

        return $flag;

    }

    

    public function alterar(Item $objItem){
       
        $flag = true;

        $DAOf = new DAOFactory();
        $objDAOItem = $DAOf->getItemDAO();

        //$flag = $this->validarAlterar($objItem);
        
        if($flag){
            $flag = $objDAOItem->atualizar($objItem);
        }

        return $flag;

    }


   

    public function deletar(Item $objItem){
          
            $flag = true;
            
            $DAOf = new DAOFactory();
            $objDAOItem = $DAOf->getItemDAO();
                        
            if($flag){
                $ok = $objDAOItem->deletar($objItem);
                }
            return $ok;
        }
	
    private function copiarPropriedades(array $array, Item $objItem){


        if (isset($array["ide_item"])) {
            $objItem->setId($array["ide_item"]);
        }
        if (isset($array["nom_item"])) {
            $objItem->setNome($array["nom_item"]);
        }
        if (isset($array["des_item"])) {
            $objItem->setDescricao($array["des_item"]);
        }
        
        return $objItem;
   }


  

}

?>